package com.ruoyi.project.dict.zi.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.ZiUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.zi.domain.Zi;
import com.ruoyi.project.dict.zi.service.IZiService;
import com.ruoyi.project.dict.zicorrectlog.domain.ZiCorrectLog;
import com.ruoyi.project.dict.zicorrectlog.service.IZiCorrectLogService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 生字管理Controller
 *
 * @author shixiangwen
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/dict/zi")
public class ZiController extends BaseController {
    private String prefix = "dict/zi";

    @Autowired
    private IZiService ziService;

    @Autowired
    private IZiCorrectLogService ziCorrectLogService;

    @RequiresPermissions("dict:zi:view")
    @GetMapping()
    public String zi() {
        return prefix + "/zi";
    }

    /**
     * 查询生字管理列表
     */
    @RequiresPermissions("dict:zi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Zi zi) {
        startPage();
        List<Zi> list = ziService.selectZiList(zi);
        return getDataTable(list);
    }

    /**
     * 导出生字管理列表
     */
    @RequiresPermissions("dict:zi:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Zi zi) {
        List<Zi> list = ziService.selectZiList(zi);
        ExcelUtil<Zi> util = new ExcelUtil<Zi>(Zi.class);
        return util.exportExcel(list, "zi");
    }

    /**
     * 新增生字管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存生字管理
     */
    @RequiresPermissions("dict:zi:add")
    @Log(title = "生字管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Zi zi) {
        return toAjax(ziService.insertZi(zi));
    }

    /**
     * 修改生字管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Zi zi = ziService.selectZiById(id);
        mmap.put("zi", zi);
        return prefix + "/edit";
    }

    @GetMapping("/compare/{id}")
    public String compare(@PathVariable("id") Long id, ModelMap mmap) {
        Zi zi = ziService.selectZiById(id);
        mmap.put("zi", zi);

        if (StringUtils.isNotEmpty(zi.getXiangjieJson())) {
            mmap.put("xiangjieJson", JsonUtil.toListObject(zi.getXiangjieJson(), Map.class));
        }

        return prefix + "/compare";
    }

    @GetMapping("/aiCompare/{id}")
    public String aiCompare(@PathVariable("id") Long id, ModelMap mmap) {
        Zi zi = ziService.selectZiById(id);
        mmap.put("zi", zi);

        if (StringUtils.isNotEmpty(zi.getXiangjieAiJson())) {
            List<Map> data = JsonUtil.toListObject(zi.getXiangjieAiJson(), Map.class);
            mmap.put("xiangjieAiJson", data);
        }
        if (StringUtils.isNotEmpty(zi.getXiangjieJson())) {
            mmap.put("xiangjieJson", JsonUtil.toListObject(zi.getXiangjieJson(), Map.class));
        }

        return prefix + "/aiCompare";
    }


    @GetMapping("/html/{id}")
    public String html(@PathVariable("id") Long id, Integer type, ModelMap mmap) {
        Zi zi = ziService.selectZiById(id);
        mmap.put("zi", zi);

        if (StringUtils.isNotEmpty(zi.getXiangjieJson())) {
            mmap.put("xiangjieJson", JsonUtil.toListObject(zi.getXiangjieJson(), Map.class));
        }

        type = type == null ? 1 : type;

        if (type == 1) {
            return prefix + "/htmlRaw";
        } else {
            return prefix + "/htmlJson";
        }
    }

    @GetMapping("/xiangjieJsonEdit/{id}")
    public String xiangjieJsonEdit(@PathVariable("id") Long id, ModelMap mmap) {
        Zi zi = ziService.selectZiById(id);
        mmap.put("zi", zi);
        return prefix + "/xiangjieJsonEdit";
    }

    @GetMapping("/guhanyuJsonEdit/{id}")
    public String guhanyuJsonEdit(@PathVariable("id") Long id, ModelMap mmap) {
        Zi zi = ziService.selectZiById(id);
        mmap.put("zi", zi);
        return prefix + "/guhanyuJsonEdit";
    }

    @GetMapping("/kkcJsonEdit/{id}")
    public String kkcJsonEdit(@PathVariable("id") Long id, ModelMap mmap) {
        Zi zi = ziService.selectZiById(id);
        mmap.put("zi", zi);
        return prefix + "/kkcJsonEdit";
    }

    /**
     * 修改保存生字管理
     */
    @RequiresPermissions("dict:zi:edit")
    @Log(title = "生字管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Zi zi) {
        Zi ziOld = ziService.selectZiById(zi.getId());
        String content = ZiUtil.diff(ziOld, zi);
        if (!StringUtils.isEmpty(content)) {
            ZiCorrectLog ziCorrectLog = new ZiCorrectLog();
            ziCorrectLog.setZiBytes(ziOld.getZiBytes());
            ziCorrectLog.setZi(ziOld.getZi());
            ziCorrectLog.setContent(content);
            ziCorrectLog.setCreatedTime(DateUtils.getNowDate());
            ziCorrectLog.setCreatedUser(ShiroUtils.getLoginName());
            ziCorrectLogService.insertZiCorrectLog(ziCorrectLog);
        }
        return toAjax(ziService.updateZi(zi));
    }

    /**
     * 删除生字管理
     */
    @RequiresPermissions("dict:zi:remove")
    @Log(title = "生字管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ziService.deleteZiByIds(ids));
    }
}
